#include "GameLogic.h"
#include "qipan.h"
#include <QDebug>
GameLogic::GameLogic(QWidget *parent) : QWidget(parent)
{

}

bool GameLogic::checkWin(int row, int col, Role role)
{
    //四个方向
    const int directions[4][2] =
    {
        //水平
        {1, 0},
        //垂直
        {0, 1},
        //左对角
        {1, 1},
        //右对角
        {1, -1}
    };

    //四个方向探测
    for (int direct = 0; direct < 4; direct++)
    {
        //初始化计数器
        int cnt = 1;

        //向正方向探测
        for (int idx = 1; idx < 5; idx++)
        {
            int newRow = row + directions[direct][0] * idx;
            int newCol = col + directions[direct][1] * idx;

            if(newRow >= 0 && newRow < m_boardSize && newCol >= 0 && newCol < m_boardSize && m_board[newRow][newCol] == role)
            {
                cnt++;
            }
            else
            {
                break;
            }
        }

        //向正方向探测
        for (int idx = 1; idx < 5; idx++)
        {
            int newRow = row - directions[direct][0] * idx;
            int newCol = col - directions[direct][1] * idx;

            if(newRow >= 0 && newRow < m_boardSize && newCol >= 0 && newCol < m_boardSize && m_board[newRow][newCol] == role)
            {
                cnt++;
            }
            else
            {
                break;
            }
        }

        if(cnt >= 5)
        {
            return true;
        }
    }

    return false;
}

void GameLogic::setQipanBoard(Role **board, int boardSize)
{
    m_board = board;
    m_boardSize = boardSize;
}

void GameLogic::ComputerMove()
{
    int bestScore = 0;
    int bestRow = 0;
    int bestCol = 0;
    int Score = 0;
    //int boardSize = m_boardSize;

    for (int row = 0; row < m_boardSize; row++)
    {

        for (int col = 0; col < m_boardSize; col++)
        {
            if(m_board[row][col] == 0)
            {
                // 判断这个点对AI来说是不是最有利的
                // 1、AI能赢
                // 2、对玩家不利（对玩家来说是最有利的）
                Score = evaluateMove(row, col, COMPUTER) + evaluateMove(row, col, PLAYER);

                if(bestScore < Score)
                {
                    bestScore = Score;
                    bestRow = row;
                    bestCol  = col;
                }
            }
        }
    }

    //发送信号
    qDebug() << bestRow << bestCol << bestScore << endl;
    emit AiPostion(bestRow, bestCol);
}

int GameLogic::evaluateMove(int row, int col, Role role)
{
    int Score = 0;
    //连子数 + 开放端
    //四个方向
    const int directions[4][2] =
    {
        //水平
        {1, 0},
        //垂直
        {0, 1},
        //左对角
        {1, 1},
        //右对角
        {1, -1}
    };

    //四个方向探测
    for (int direct = 0; direct < 4; direct++)
    {
        //连字数
        int count = 0;

        //开放端
        int openEnds = 0;

        //向一个方向检查最多四个棋子
        for (int idx = 1; idx <= 4; idx ++)
        {
            int newRow = row + directions[direct][0] * idx;
            int newCol = col + directions[direct][1] * idx;

            //检测新坐标是否在棋盘内
            if (newRow >= 0 && newRow < m_boardSize &&
                newCol >= 0 && newCol < m_boardSize)
            {
                if(m_board[newRow][newCol] == role)
                {
                    count++;
                }
                else if (m_board[newRow][newCol] == EMPTY)
                {
                    openEnds++;
                    break;
                }
                else
                {
                    //遇到对手的棋子
                    break;
                }
            }
            else
            {
                break;
            }
        }

        //向一个方向检查最多四个棋子
        for (int idx = 1; idx <= 4; idx ++)
        {
            int newRow = row - directions[direct][0] * idx;
            int newCol = col - directions[direct][1] * idx;

            //检测新坐标是否在棋盘内
            if (newRow >= 0 && newRow < m_boardSize &&
                newCol >= 0 && newCol < m_boardSize)
            {
                if(m_board[newRow][newCol] == role)
                {
                    count++;
                }
                else if (m_board[newRow][newCol] == EMPTY)
                {
                    openEnds++;
                    break;
                }
                else
                {
                    //遇到对手的棋子
                    break;
                }
            }
            else
            {
                break;
            }
        }

        if(count == 4 && openEnds > 0)
        {
            Score += 10000;
        }
        else if (count == 3 && openEnds > 0)
        {
            Score += 1000;
        }
        else if (count == 2 && openEnds > 0)
        {
            Score += 100;
        }
        else if (count == 1 && openEnds > 0)
        {
            Score += 10;
        }


    }

    return Score;
}
